package MathText
{
	/** Stores information about the subject of a math sentence. e.g. initial quantity of stuff, type of stuff, etc.
	 */ 
	public class Subject
	{
		public static const VALUE_NOT_SET:int = -999;
		public static const VALUE_TO_BE_DETERMINED:int = -1000;
		// most stuff is public
		public var id:int;  // id number presented in problemTemplate e.g., subject_1 is 1.
		public var name:String=""; // name of subject
		public var pronoun:String; // pronoun of subject, e.g. he she it
		public var opronoun:String; // object pronoun of subject, e.g. him or her, them
		public var plural:Boolean = false; // is this subject a group, e.g., Joe and Tom
		public var first_person:Boolean = false; // Are we talking about ourself, I?
		public var iquantity:Number = VALUE_NOT_SET; // the initial quantity of this subject
		public var cquantity:Number = VALUE_NOT_SET; // the quantity being exchanged
		public var fquantity:Number = VALUE_NOT_SET; // the final quanity of this subject
		public var iquantity_unit:String=""; // the unit (object name), which could be singular or plural, at initial state
		public var cquantity_unit:String=""; // "  " exchange
		public var fquantity_unit:String=""; // "  " final state
		public var pverb_present:String="";  // the verb of possesion, "has"
		public var pverb_past:String="";  // the past verb of possesion, "had"
		public var pverb_future:String="";  // the futre verb of possesion, "have"
		public var averb_present:String="";  // the action verb, e.g. "give"	
		public var averb_past:String="";  // the past action verb, e.g. "gave"	
		public var averb_future:String="";  // the past action verb, e.g. "give"	
		
		private var subjectSet:Boolean = false;
		
		public function Subject(id:int)
		{
			this.id = id;
		}
		/** A subject node includes a name and a pronoun, maybe plural, maybe first-person (probably not) */ 
		public function setSubject(subjectXML:XML):void
		{
			subjectSet = true;
			if (subjectXML..name.length()>0) name = subjectXML..name[0];
			if (subjectXML..pronoun.length()>0) pronoun = subjectXML..pronoun[0];
			if (subjectXML..opronoun.length()>0) opronoun = subjectXML..opronoun[0];
			if (subjectXML..plural.length()>0 && subjectXML..plural[0]=="true") plural = true;
			if (subjectXML..first_person.length()>0 && subjectXML..first_person[0]=="true") first_person = true;
		}
		/** Set both the present and past version of the passive, possession verb.  Name should be set first. */
		public function setPVerb (pverbXML:XML):void
		{
			if (subjectSet)
			{
				if (!plural && !first_person)
				{
					if (pverbXML..present_third_singular.length() > 0){ pverb_present = pverbXML..present_third_singular[0]; } else { trace ("Need a present_third_singular pverb.") }
					if (pverbXML..past_third_singular.length() > 0){ pverb_past = pverbXML..past_third_singular[0]; } else { trace ("Need a past_third_singular pverb.") }
					if (pverbXML..future_third_singular.length() > 0){ pverb_future = pverbXML..future_third_singular[0]; } else { trace ("Need a future_third_singular pverb.") }
				} else if (plural && !first_person)
				{
					if (pverbXML..present_third_plural.length() > 0){ pverb_present = pverbXML..present_third_plural[0]; } else { trace ("Need a present_third_plural pverb.") }
					if (pverbXML..past_third_plural.length() > 0){ pverb_past = pverbXML..past_third_plural[0]; } else { trace ("Need a past_third_plural pverb.") }	
					if (pverbXML..future_third_plural.length() > 0){ pverb_future = pverbXML..future_third_plural[0]; } else { trace ("Need a future_third_plural pverb.") }	
				} else if (!plural && first_person)
				{
					if (pverbXML..present_first_singular.length() > 0){ pverb_present = pverbXML..present_first_singular[0]; } else { trace ("Need a present_first_singular pverb.") }
					if (pverbXML..past_first_singular.length() > 0){ pverb_past = pverbXML..past_first_singular[0]; } else { trace ("Need a past_first_singular pverb.") }	
					if (pverbXML..future_first_singular.length() > 0){ pverb_future = pverbXML..future_first_singular[0]; } else { trace ("Need a future_first_singular pverb.") }	
				} else if (plural && first_person)
				{
					if (pverbXML..present_first_plural.length() > 0){ pverb_present = pverbXML..present_first_plural[0]; } else { trace ("Need a present_first_plural pverb.") }
					if (pverbXML..past_first_plural.length() > 0){ pverb_past = pverbXML..past_first_plural[0]; } else { trace ("Need a past_first_plural pverb.") }	
					if (pverbXML..future_first_plural.length() > 0){ pverb_future = pverbXML..future_first_plural[0]; } else { trace ("Need a future_first_plural pverb.") }	
				} else 
				{
					trace("Impossible!");
				}
			} else 
			{
				trace ("Set subject before pverb.");
			}
		}
		/** Set both the present and past version of the passive, possession verb */
		public function setAVerb (averbXML:XML):void
		{
			if (subjectSet)
			{
				if (!plural && !first_person)
				{
					if (averbXML..present_third_singular.length() > 0){ averb_present = averbXML..present_third_singular[0]; } else { trace ("Need a present_third_singular averb.") }
					if (averbXML..past_third_singular.length() > 0){ averb_past = averbXML..past_third_singular[0]; } else { trace ("Need a past_third_singular averb.") }	
					if (averbXML..future_third_singular.length() > 0){ averb_future = averbXML..future_third_singular[0]; } else { trace ("Need a future_third_singular averb.") }	
				} else if (plural && !first_person)
				{
					if (averbXML..present_third_plural.length() > 0){ averb_present = averbXML..present_third_plural[0]; } else { trace ("Need a present_third_plural averb.") }
					if (averbXML..past_third_plural.length() > 0){ averb_past = averbXML..past_third_plural[0]; } else { trace ("Need a past_third_plural averb.") }	
					if (averbXML..future_third_plural.length() > 0){ averb_future = averbXML..future_third_plural[0]; } else { trace ("Need a future_third_plural averb.") }	
				} else if (!plural && first_person)
				{
					if (averbXML..present_first_singular.length() > 0){ averb_present = averbXML..present_first_singular[0]; } else { trace ("Need a present_first_singular averb.") }
					if (averbXML..past_first_singular.length() > 0){ averb_past = averbXML..past_first_singular[0]; } else { trace ("Need a past_first_singular averb.") }
					if (averbXML..future_first_singular.length() > 0){ averb_future = averbXML..future_first_singular[0]; } else { trace ("Need a future_first_singular averb.") }
				} else if (plural && first_person)
				{
					if (averbXML..present_first_plural.length() > 0){ averb_present = averbXML..present_first_plural[0]; } else { trace ("Need a present_first_plural averb.") }
					if (averbXML..past_first_plural.length() > 0){ averb_past = averbXML..past_first_plural[0]; } else { trace ("Need a past_first_plural averb.") }
					if (averbXML..future_first_plural.length() > 0){ averb_future = averbXML..future_first_plural[0]; } else { trace ("Need a future_first_plural averb.") }
				} else 
				{
					trace("Impossible!");
				}
			} else
			{
				trace ("Set subject before averb.");
			}
		}	
		/** Update the quantity field, but attend to time period "i" (initial), "c" (exchange), "f" (final) */
		public function setQuantity(value:Number, time:String="i"):void
		{
			if (time == "i")
			{
				iquantity = value;
			} else if (time == "c")
			{
				cquantity = value;
			} else if (time == "f")
			{
				fquantity = value;
			} else 
			{
				trace ("Not a correct time input,", time);
			}
		}
		
		/** Get the appropriate quantity for the given time */
		public function getQuantity (time:String = "i"):Number
		{
			if (time == "i") { return iquantity; }
			else if (time == "c") { return cquantity; }
			else if (time == "f") { return fquantity; }
			else { trace ("Not an appropriate time unit", time); return Subject.VALUE_NOT_SET}
		}
		//// FOR THE FOLLOWING METHODS THE QUANTITY SHOULD BE SET FIRST
		/** Set the unit name for a quantity, check the quantity of the given time, if > 1 use plural, else use singular */
		public function setUnit (unitXML:XML, time:String = "i"):void
		{
			if (time == "i" )
			{
				if (iquantity == 1)
				{
					if (unitXML..singular.length() > 0){ iquantity_unit = unitXML..singular[0]; } else { trace ("Need a singular case for initial quanity unit"); }
				} else
				{
					if (unitXML..plural.length() > 0){ iquantity_unit = unitXML..plural[0]; } else { trace ("Need a plural case for initial quanity unit"); }
				}
			} else if (time == "c" )
			{
				if (cquantity == 1)
				{
					if (unitXML..singular.length() > 0){ cquantity_unit = unitXML..singular[0]; } else { trace ("Need a singular case for exchange quanity unit"); }
				} else
				{
					if (unitXML..plural.length() > 0){ cquantity_unit = unitXML..plural[0]; } else { trace ("Need a plural case for exchange quanity unit"); }
				}
			} else if (time == "f" )
			{
				if (fquantity == 1)
				{
					if (unitXML..singular.length() > 0){ fquantity_unit = unitXML..singular[0]; } else { trace ("Need a singular case for final quanity unit"); }
				} else
				{
					if (unitXML..plural.length() > 0){ fquantity_unit = unitXML..plural[0]; } else { trace ("Need a plural case for final quanity unit"); }
				}
			} else 
			{
				trace ("You do not have a quantity set at this time,", time);
			}
		}
		/** Get the appropriate unit for the given time */
		public function getUnit (time:String = "i"):String
		{
			if (time == "i") { return iquantity_unit; }
			else if (time == "c") { return cquantity_unit; }
			else if (time == "f") { return fquantity_unit; }
			else { trace ("Not an appropriate time unit", time); return ""}
		}
		public function getAnyUnit():String
		{
			if (iquantity_unit.length > 0)
			{
				return iquantity_unit;
			} else if (cquantity_unit.length > 0)
			{
				return cquantity_unit;
			} else if (fquantity_unit.length > 0)
			{
				return fquantity_unit;
			} else
			{
				return "unit";
			}
		}
		public function getAnyAVerb():String
		{
			if (averb_present.length > 0)
			{
				return averb_present;
			} else if (averb_past.length > 0)
			{
				return averb_past;
			} else if (averb_future.length > 0)
			{
				return averb_future;
			} else
			{
				return "change";
			}
		}
		public function print():void
		{
			trace(this.id, this.name, this.iquantity, this.iquantity_unit, this.pverb_present, this.averb_present);
		}
	}
}